﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using BUS;
namespace CustomerManager
{
    public partial class ucPersonal : UserControl
    {
        Int32 manhanvien;
        public void ChiTietCaNhan(Personal kh)
        {
            txtMaKhachHang.Text = kh.Ma;
            txtDiaChi.Text = kh.DiaChi;
            txtHanMucTinDung.Text = kh.HanMuc.ToString();
            txtMaSoThue.Text = kh.MaSoThue;
            txtNamCongTac.Text = kh.SoNamLamViec.ToString();
            txtNgheNghiep.Text = kh.NgheNghiep;
            txtNoiCongTac.Text = kh.NoiLamViec;
            txtNoiDangKyHoKhau.Text = kh.HoKhau;
            txtSoCMND.Text = kh.CMND;
            txtSoDienThoai.Text = kh.DienThoai;
            txtSoNhanKhauPhuThuoc.Text = kh.SoNguoiPhuThuoc.ToString();
            cmbTrinhDoChuyenMon.Text = kh.TrinhDo;
            txtTenKhachHang.Text = kh.Ten;
            txtThuNhapBinhQuan.Text = kh.ThuNhap.ToString();
            txtThuNhapGiaDinh.Text = kh.ThuNhapGiaDinh.ToString();
            txtTinhTrang.Text = kh.HonNhan;
            manhanvien = kh.MaNhanVien;
        }

        private void ValidateControls(bool enable)
        {
            txtDiaChi.Enabled = enable;
            txtHanMucTinDung.Enabled = enable;
            txtMaKhachHang.Enabled = enable;
            txtMaSoThue.Enabled = enable;
            txtNamCongTac.Enabled = enable;
            txtNgheNghiep.Enabled = enable;
            txtNoiCongTac.Enabled = enable;
            txtNoiDangKyHoKhau.Enabled = enable;
            txtSoCMND.Enabled = enable;
            txtSoDienThoai.Enabled = enable;
            txtSoNhanKhauPhuThuoc.Enabled = enable;
            txtTenKhachHang.Enabled = enable;
            txtThuNhapBinhQuan.Enabled = enable;
            txtThuNhapGiaDinh.Enabled = enable;
            txtTinhTrang.Enabled = enable;
            dtpNamSinh.Enabled = enable;
            cmbTrinhDoChuyenMon.Enabled = enable;
        }
        ucCustomer frm;
        public ucPersonal(FrmMain.ExeAction act, ucCustomer f)
        {
            InitializeComponent();
            frm = f;
            if (act == FrmMain.ExeAction.Add)
            {
                btnSua.Visible = false;
                btnXoa.Visible = false;
                ValidateControls(true);
            }
            else if(act == FrmMain.ExeAction.View)
            {
                btnLuu.Visible = false;
                ValidateControls(false);
            }
        }

        private void ucPersonal_Load(object sender, EventArgs e)
        {

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (btnSua.Text == "Sửa thông tin")
            {
                ValidateControls(true);
                txtMaKhachHang.Enabled = false;
                btnSua.Text = "Lưu thay đổi";
            }
            else if (btnSua.Text == "Lưu thay đổi")
            {
                try
                {
                    Personal person = new Personal();
                    person.CMND = txtSoCMND.Text;
                    person.DiaChi = txtDiaChi.Text;
                    person.DienThoai = txtSoDienThoai.Text;
                    person.HanMuc = Int32.Parse(txtHanMucTinDung.Text);
                    person.HoKhau = txtNoiDangKyHoKhau.Text;
                    person.HonNhan = txtTinhTrang.Text;
                    person.Ma = txtMaKhachHang.Text;
                    person.MaNhanVien = manhanvien;
                    person.MaSoThue = txtMaSoThue.Text;
                    person.NgaySinh = dtpNamSinh.Value;
                    person.NgheNghiep = txtNgheNghiep.Text;
                    person.NoiLamViec = txtNoiCongTac.Text;
                    person.SoNamLamViec = Int32.Parse(txtNamCongTac.Text);
                    person.SoNguoiPhuThuoc = Int32.Parse(txtSoNhanKhauPhuThuoc.Text);
                    person.Ten = txtTenKhachHang.Text;
                    person.ThuNhap = Int32.Parse(txtThuNhapBinhQuan.Text);
                    person.ThuNhapGiaDinh = Int32.Parse(txtThuNhapGiaDinh.Text);
                    person.TrinhDo = cmbTrinhDoChuyenMon.Text;

                    PersonalBUS.CapNhatKHCaNhan(person);

                    MessageBox.Show("Thay đổi thành công!");
                    ValidateControls(false);
                    frm.LoadCustomer(manhanvien);
                    btnSua.Text = "Sửa thông tin";
                }
                catch (Exception ex) 
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnTrolai_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                String makhcanhan = txtMaKhachHang.Text;
                if (MessageBox.Show("Bạn có chắc xóa khách hàng này không?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    PersonalBUS.XoaKHCaNhan(makhcanhan);
                    MessageBox.Show("Xóa thành công");
                    frm.LoadCustomer(manhanvien);
                    this.Dispose();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                Personal person = new Personal();
                person.CMND = txtSoCMND.Text;
                person.DiaChi = txtDiaChi.Text;
                person.DienThoai = txtSoDienThoai.Text;
                person.HanMuc = Int32.Parse(txtHanMucTinDung.Text);
                person.HoKhau = txtNoiDangKyHoKhau.Text;
                person.HonNhan = txtTinhTrang.Text;
                person.Ma = txtMaKhachHang.Text;
                person.MaNhanVien = manhanvien;
                person.MaSoThue = txtMaSoThue.Text;
                person.NgaySinh = dtpNamSinh.Value;
                person.NgheNghiep = txtNgheNghiep.Text;
                person.NoiLamViec = txtNoiCongTac.Text;
                person.SoNamLamViec = Int32.Parse(txtNamCongTac.Text);
                person.SoNguoiPhuThuoc = Int32.Parse(txtSoNhanKhauPhuThuoc.Text);
                person.Ten = txtTenKhachHang.Text;
                person.ThuNhap = Int32.Parse(txtThuNhapBinhQuan.Text);
                person.ThuNhapGiaDinh = Int32.Parse(txtThuNhapGiaDinh.Text);
                person.TrinhDo = cmbTrinhDoChuyenMon.Text;

                PersonalBUS.ThemKHCaNhan(person);

                MessageBox.Show("Thêm khách hàng thành công!");
                ValidateControls(false);
                frm.LoadCustomer(manhanvien);
                btnLuu.Hide();
                btnSua.Text = "Sửa thông tin";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }

      
}
